<template>
  <div ref="canvas"></div>
</template>

<script lang="ts" setup>
import * as THREE from "three";
import Stats from "three/examples/jsm/libs/stats.module";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { ref, onMounted } from "vue";
import {PLYLoader} from "three/examples/jsm/loaders/PLYLoader";

const canvas = ref<any>();

const scene = new THREE.Scene();

const renderer = new THREE.WebGLRenderer({ antialias : true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;

const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight);
camera.position.set(30, 30, 30);
camera.lookAt(0, 0, 0);

const orbitControl = new OrbitControls(camera, renderer.domElement);
orbitControl.update();

const stat = Stats();

const keyLight = new THREE.SpotLight(0xffffff);
keyLight.position.set(0, 80, 80);
keyLight.intensity = 2;
keyLight.lookAt(new THREE.Vector3(0, 15, 0));
keyLight.castShadow = true;
keyLight.shadow.mapSize.height = 4096;
keyLight.shadow.mapSize.width = 4096;
scene.add(keyLight);

const backlight1 = new THREE.SpotLight(0xaaaaaa);
backlight1.position.set(150, 40, -20);
backlight1.intensity = 0.5;
backlight1.lookAt(new THREE.Vector3(0, 15, 0));
scene.add(backlight1);

const backlight2 = new THREE.SpotLight(0xaaaaaa);
backlight2.position.set(-150, 40, -20);
backlight2.intensity = 0.5;
backlight2.lookAt(new THREE.Vector3(0, 15, 0));
scene.add(backlight2);

const loader = new PLYLoader();
loader.load(
    new URL("@/assets/models/carcloud/carcloud.ply", import.meta.url).href,
    (geom) => {
      const material = new THREE.PointsMaterial({
        color: 0xffffff,
        size: 1,
        opacity: 0.6,
        transparent: true,
        blending: THREE.AdditiveBlending,
        depthWrite: false,
        map: generateSprite()
      });
      const group = new THREE.Points(geom, material);
      group.scale.set(2.5, 2.5, 2.5);
      scene.add(group);
    }
)

const generateSprite = () => {
  const canvas = document.createElement('canvas');
  canvas.width = 16;
  canvas.height = 16;
  const context = canvas.getContext('2d');

  // draw the sprites
  const gradient = context.createRadialGradient(canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2);
  gradient.addColorStop(0, 'rgba(255,255,255,1)');
  gradient.addColorStop(0.2, 'rgba(0,255,255,1)');
  gradient.addColorStop(0.4, 'rgba(0,0,64,1)');
  gradient.addColorStop(1, 'rgba(0,0,0,1)');
  context.fillStyle = gradient;
  context.fillRect(0, 0, canvas.width, canvas.height);

  // create the texture
  const texture = new THREE.Texture(canvas);
  texture.needsUpdate = true;
  return texture;
};

const render = () => {
  stat.update();

  requestAnimationFrame(render);
  renderer.render(scene, camera);
};

onMounted(() => {
  canvas.value.appendChild(stat.domElement);
  canvas.value.appendChild(renderer.domElement)
  render();
})

</script>
